package com.example.jk.middlewares;

import com.example.jk.core.Context;
import com.example.jk.core.Koa;
import com.example.jk.core.KoaPlugin;
import com.example.jk.core.Log;
import com.example.jk.core.Next;

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;

public class UndertowServerPlugin extends KoaPlugin {

	Koa koa;

	public UndertowServerPlugin(Koa koa) {
		this.koa = koa;
	}


	int i;

	@Override
	public void init() {
		HttpHandler handler = exchange -> {
			Log.info("收到请求,访问计数:{},HttpServerExchange:{}", i++, exchange);

			HttpServerContext context = new HttpServerContext();

			context.requestPath = exchange.getRequestPath();
			context.method = exchange.getRequestMethod().toString();

			context.exchange = exchange;
			
			
			koa.executeMiddlewares(context);
//			koa.handleRequest(context,new Next() {
//				
//				@Override
//				public void handle(Context ctx) {
//					
//				}
//			});
		};

		Undertow.builder().addHttpListener(8080, "localhost").setHandler(handler).build().start();
	}
}